//
//  SCNSigInGoldTreeView.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/5/23.
//  Copyright © 2017年 Riluee. All rights reserved.
//
/*
 这里还有部分的逻辑没有处理
 心情好了再写 5-25
 */
import UIKit
import Kingfisher

class SCNSigInGoldTreeView: UIView {

    // 返回
    var backAction: (() -> Void)?
    // 规则
    var ruleHandleAction: (() -> Void)?
    
    /// 🌲图 1
    lazy var treeImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "goldtree")
        return imgView
    }()
    /// 返回首页 2
    lazy var backBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-fhsyh"), for: .normal)
        btn.addTarget(self, action: #selector(back), for: .touchUpInside)
        return btn
    }()
    /// 养护记录 3
    lazy var recordBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-yhrz"), for: .normal)
        btn.addTarget(self, action: #selector(recordHandle), for: .touchUpInside)
        return btn
    }()
    /// 规则 4
    lazy var ruleBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-gz"), for: .normal)
        btn.addTarget(self, action: #selector(wateringRuleHandle), for: .touchUpInside)
        return btn
    }()
    /// 浇水 5
    lazy var wateringBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-js"), for: .normal)
        btn.addTarget(self, action: #selector(wateringHandle), for: .touchUpInside)
        return btn
    }()
    
    /// gif 6
    lazy var wateingGifImgView: AnimatedImageView = {
        let imgView = AnimatedImageView()
        
        let path = Bundle.main.path(forResource: "wateringGif", ofType: "gif")
        let url = URL(fileURLWithPath: path!)
        imgView.kf.setImage(with: url)
        imgView.isHidden = true
        return imgView
    }()
    
    /// gold image 7
    lazy var goldImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "goldO")
        imgView.isHidden = true
        return imgView
    }()
    /// gold text 8
    lazy var goldL: UILabel = {
      
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWith255RGBA(81, g: 135, b: 48, a: 1)
        lab.font = UIFont.systemFont(ofSize: 14.0)
        lab.textAlignment = .center
        lab.text = "+6"
        return lab
    }()
    /// hint 9
    lazy var hintL1: SCNGoldTreeHintView = {
       
        let view = SCNGoldTreeHintView()
        view.hintText = "理财树规则"
        return view
    }()
    lazy var hintL2: SCNGoldTreeHintView = {
        
        let view = SCNGoldTreeHintView()
        view.hintText = "养护记录"
        return view
    }()
    lazy var hintL3: SCNGoldTreeHintView = {
        
        let view = SCNGoldTreeHintView()
        view.hintText = "浇水"
        return view
    }()
    /// 红包
    lazy var hongbaoBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-hongbao"), for: .normal)
        btn.addTarget(self, action: #selector(wateringHandle), for: .touchUpInside)
//        btn.isHidden = true
        return btn
    }()
    /// 铜果子1
    lazy var tong1Btn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-tongguozi"), for: .normal)
        btn.addTarget(self, action: #selector(wateringHandle), for: .touchUpInside)
//        btn.isHidden = true
        return btn
    }()
    /// 铜果子2
    lazy var tong2Btn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-tongguozi"), for: .normal)
        btn.addTarget(self, action: #selector(wateringHandle), for: .touchUpInside)
//        btn.isHidden = true
        return btn
    }()
    /// 银果子
    lazy var yinBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-yingguozi"), for: .normal)
        btn.addTarget(self, action: #selector(wateringHandle), for: .touchUpInside)
//        btn.isHidden = true
        return btn
    }()
    /// 金果子
    lazy var jinBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named:"icon-jinguozi"), for: .normal)
        btn.addTarget(self, action: #selector(wateringHandle), for: .touchUpInside)
//        btn.isHidden = true
        return btn
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(treeImgView)
        addSubview(backBtn)
        addSubview(recordBtn)
        addSubview(ruleBtn)
        addSubview(wateringBtn)
        addSubview(wateingGifImgView)
        addSubview(goldImgView)
        goldImgView.addSubview(goldL)
        addSubview(hintL1)
        addSubview(hintL2)
        addSubview(hintL3)
        addSubview(hongbaoBtn)
        addSubview(tong1Btn)
        addSubview(tong2Btn)
        addSubview(yinBtn)
        addSubview(jinBtn)
        
        
        treeImgView.snp.makeConstraints { (make) in

            make.edges.equalTo(self)
        }
        backBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 80,height: 32))
            make.leading.equalTo(25)
            make.top.equalTo(40)
        }
        recordBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 61,height: 61))
            make.bottom.equalTo(-50)
            make.trailing.equalTo(-25)
        }
        ruleBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 48,height: 48))
            make.leading.equalTo(40)
            make.bottom.equalTo(-45)
        }
        wateringBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 48,height: 48))
            make.bottom.equalTo(-45)
            make.centerX.equalTo(self)
        }
        wateingGifImgView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 61,height: 46));
            make.bottom.equalTo(-210);
            make.leading.equalTo(self.snp.centerX).offset(40);
        }
        goldImgView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self).offset(25)
            make.top.equalTo(90)
            make.size.equalTo(CGSize(width: 65,height: 65))
        }
        goldL.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.centerX.equalTo(goldImgView)
            make.size.equalTo(CGSize(width: 45,height: 20))
        }
        hintL1.snp.makeConstraints { (make) in
            make.bottom.equalTo(ruleBtn.snp.top)
            make.centerX.equalTo(ruleBtn)
            make.size.equalTo(CGSize(width: 70,height: 30))
        }
        hintL2.snp.makeConstraints { (make) in
            make.bottom.equalTo(recordBtn.snp.top)
            make.centerX.equalTo(recordBtn)
            make.size.equalTo(CGSize(width: 58,height: 30))
        }
        hintL3.snp.makeConstraints { (make) in
            make.bottom.equalTo(wateringBtn.snp.top)
            make.centerX.equalTo(wateringBtn)
            make.size.equalTo(CGSize(width: 34,height: 30))
        }
        hongbaoBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 54,height: 66))
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-65)
        }
        tong1Btn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53,height: 53))
            make.centerX.equalTo(self).offset(-50)
            make.centerY.equalTo(self).offset(-90)
        }
        tong2Btn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53,height: 53))
            make.centerX.equalTo(self).offset(48)
            make.centerY.equalTo(self).offset(-120)
        }
        yinBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53,height: 53))
            make.centerX.equalTo(self).offset(-60)
            make.centerY.equalTo(self).offset(-10)
        }
        jinBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 53,height: 53))
            make.centerX.equalTo(self).offset(50)
            make.centerY.equalTo(self).offset(-10)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
extension SCNSigInGoldTreeView {
    
    func back() {
        backAction?()
    }
    
    func recordHandle() {
        
    }
    
    func wateringRuleHandle() {
        ruleHandleAction?()
    }
    
    func wateringHandle() {
        
        SCNRequestManager.postRequest([:], URL: SCNRequestAPI.investTree().doWaterTree) { [unowned self] (result) in
            
            let value = result.result.value as? Dictionary<String, Any>
            let dataresult = value?["dataresult"] as? Dictionary<String, Any>
            let integral = dataresult?["integral"]
            let info = dataresult?["info"]
            if let tmp = integral {
                self.goldL.text = "+\(String(describing: tmp))"
            }
            
            self.goldImgView.isHidden = false
            self.wateingGifImgView.isHidden = false
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.6) {
                
                self.wateingGifImgView.isHidden = true
                self.goldImgView.isHidden = true
                
                if let tmp = info {
                    self.noticeOnlyText(String(describing: tmp))
                }
            }
        }
        
        
    }
}
